<?php
	class the_model_meta extends the_model_sql {
		function get_meta($object_id, $meta_key = false) {
			if ($this->test_installation()) {
				if (!$meta_key) {
					$prefix = $this->get_prefix();
					$q = "SELECT * FROM {$prefix}meta WHERE meta_object=$object_id";
					$o = array();
					$ms = oh($this->datasource)->get_results($q);
					if (count($ms)>0) {
						foreach($ms as $i) {
							$o[$i->meta_key] = $i->meta_value;
						}
					}
					return $o;
				}
				/*
				$args 	= $this->get_args($query);
				$prefix = $this->get_prefix();
				
				$where = array();
				foreach($args as $key=>$value) {
					$where[] = "$key='$value'";
				}
				$where = implode(" AND ",$where);				
				oh_dump($args);
				$q = "SELECT * FROM {$prefix}class WHERE $where";
				if (oh("debug.model")) oh_dump($q);
				return oh($this->datasource)->get_results($q);
				*/
			}
		}
		function set_meta($object_id,$meta_key,$meta_value) {
			global $oh_object_model;
			if (!$this->hit_meta($object_id,$meta_key)) {
				$this->new_meta($object_id,$meta_key,$meta_value);
			} else {
				if ($this->test_installation()) {				
					$prefix = $this->get_prefix();
					$q 		= "UPDATE {$prefix}meta SET meta_value='$meta_value' WHERE meta_object=$object_id AND meta_key='$meta_key'";
					oh($this->datasource)->query($q);
				}
			}
			global $oh_object_model;
		}
		function new_meta($object_id,$meta_key,$meta_value) {
			global $oh_object_model;
			if ($this->test_installation()) {
				$prefix = $this->get_prefix();
				if (!$this->hit_class($args)) {
					$q = "INSERT INTO {$prefix}meta(meta_object,meta_key,meta_value) VALUES ($object_id,'$meta_key','$meta_value')";
					oh($this->datasource)->query($q);
					oh($this->datasource)->error();
				}		
			}
			global $oh_object_model;
		}
		function del_meta($object_id,$meta_key) {
			global $oh_object_model;
			if ($this->test_installation()) {
				$prefix = $this->get_prefix();
				if ($object_id != '*') {
					$o = "meta_object='{$object_id}' AND ";
				} else {
					$o = "";
				}
				$q = "DELETE FROM {$prefix}meta WHERE $o meta_key='{$meta_key}'";				
				oh($this->datasource)->query($q);
				oh($this->datasource)->error();
			}
			global $oh_object_model;
		}
		function hit_meta($object_id,$meta_key) {
			if ($this->test_installation()) {
				$prefix = $this->get_prefix();				
				$q = "SELECT id FROM {$prefix}meta WHERE meta_object=$object_id AND meta_key='$meta_key'";
				$c = oh($this->datasource)->get_results($q);
				if (count($c) > 0) {
					return true;
				} else {
					return false;
				}
			}
		}
	}
?>
